package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.SQLException;

@Controller

public class ExController {
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public String process(IOException ex){
        return ex.getMessage();
    }

    @RequestMapping("/ex_demo")
    public String demo()throws Exception{
        double random=Math.random();
        if(random<0.25){
            throw new RuntimeException("5xx错误");
        }
        else if(random>0.5){
            throw new SQLException("ErrorPage全局异常处理");
        }else{
            throw new IOException("局部异常处理");
        }

    }
}
